package com.rs.game.player.content;

import java.util.concurrent.CopyOnWriteArrayList;
import com.rs.game.WorldTile;
import com.rs.game.player.Player;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.player.Skills;
import com.rs.game.Graphics;

/*
 * doesnt let it be extended
 */
public final class Commands {

	/*
	 * all console commands only for admin, chat commands processed if they not processed by console
	 */
	/*
	 * returns if command was processed
	 */
	public static boolean processCommand(Player player, String command, boolean console,  boolean clientCommand) {
		if(command.length() == 0) //if they used ::(nothing) theres no command
			return false;
		String[] cmd = command.toLowerCase().split(" ");
		if(player.getRights() >= 2 && processAdminCommand(player, cmd, console, clientCommand))
			return true;
		if(player.getRights() >= 1 && processModCommand(player, cmd, console, clientCommand))
			return true;
		return processNormalCommand(player, cmd, console, clientCommand);
	}
	
	
	/*
	 * extra parameters if you want to check them
	 */
	public static boolean processAdminCommand(Player player, String[] cmd, boolean console, boolean clientCommand) {
		if(cmd[0].equals("testdung")) {
			player.getControlerManager().startControler("Dungeoneering", new WorldTile(4000, 4200, 0));
			return true;
		}
		else if(cmd[0].equals("spawnnpc")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::spawnnpc npcId");
				return false;
			}
			try {
				World.spawnNPC(Integer.valueOf(cmd[1]), new WorldTile(player), -1, true);
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::spawnnpc npcId");
				return false;
			}
		}
		return false;
	}
	
	public static boolean processModCommand(Player player, String[] cmd, boolean console, boolean clientCommand) {
		return false;
	}
	public static boolean processNormalCommand(Player player, String[] cmd, boolean console, boolean clientCommand) {
		if(cmd[0].equals("test")) {
			player.getPackets().sendGameMessage("works as it should.");
			return true;
		}
		else if(cmd[0].equals("rights")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::rights value");
				return false;
			}
			try {
				player.setRights(Integer.valueOf(cmd[1]));
				player.finish();
				return true;
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::rights value");
			}
		}
		else if(cmd[0].equals("item")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::item id (optional:amount)");
				return false;
			}
			try {
				player.getInventory().addItem(Integer.valueOf(cmd[1]), cmd.length >= 3 ? Integer.valueOf(cmd[2]) : 1);
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::item id (optional:amount)");
			}
		}
		else if(cmd[0].equals("spellbook")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::spellbook id");
				return false;
			}
			try {
				player.getCombatDefinitions().setSpellBook(Integer.valueOf(cmd[1]));
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::spellbook id");
			}
		}
		else if(cmd[0].equals("tonpc")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::tonpc id(-1 for player)");
				return false;
			}
			try {
				player.getAppearence().transformIntoNPC(Integer.valueOf(cmd[1]));
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::tonpc id(-1 for player)");
			}
		}
		else if(cmd[0].equals("spawnobject")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::spawnobject id");
				return false;
			}
			try {
				int objectId = Integer.valueOf(cmd[1]);
				for(int regionId : player.getMapRegionsIds()) {
					CopyOnWriteArrayList<Integer> playersIndexes = World.getRegion(regionId).getPlayerIndexes();
					if(playersIndexes == null)
						continue;
					for(Integer playerIndex : playersIndexes) {
						Player p2 = World.getPlayers().get(playerIndex);
						if(p2 == null
								|| !p2.hasStarted()
								|| p2.hasFinished()
								|| !p2.withinDistance(p2))
							continue;
						p2.getPackets().sendSpawnedObject(new WorldObject(objectId, 10, 0, player.getX(), player.getY(), player.getPlane()));
					}
				}
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::spawnobject id");
			}
			
			player.getAppearence().setRenderEmote(-1);
		}
		else if(cmd[0].equals("master")) {
			if(cmd.length < 2) {
				for(int skill = 0; skill < 25; skill++)
					player.getSkills().addXp(skill, Skills.MAXIMUM_EXP);
				return false;
			}
			try {
				player.getSkills().addXp(Integer.valueOf(cmd[1]), Skills.MAXIMUM_EXP);
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::master skill");
			}
		}
		else if(cmd[0].equals("gfxtile")) {
			if(cmd.length < 5) {
				player.getPackets().sendPainelBoxMessage("Use: ::gfxtile id x y plane");
				return false;
			}
			try {
				for(int regionId : player.getMapRegionsIds()) {
					CopyOnWriteArrayList<Integer> playersIndexes = World.getRegion(regionId).getPlayerIndexes();
					if(playersIndexes == null)
						continue;
					for(Integer playerIndex : playersIndexes) {
						Player p2 = World.getPlayers().get(playerIndex);
						if(p2 == null
								|| !p2.hasStarted()
								|| p2.hasFinished()
								|| !p2.withinDistance(p2))
							continue;
						p2.getPackets().sendGraphics(new Graphics(Integer.valueOf(cmd[1])), new WorldTile(Integer.valueOf(cmd[2]), Integer.valueOf(cmd[3]), Integer.valueOf(cmd[4])));
					}
				}
				
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::gfxtile id x y plane");
			}
		}
		else if(cmd[0].equals("tele")) {
			if(cmd.length < 3) {
				player.getPackets().sendPainelBoxMessage("Use: ::tele coordX coordY");
				return false;
			}
			try {
				player.resetWalkSteps();
				player.setNextWorldTile(new WorldTile(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]), cmd.length >= 4 ? Integer.valueOf(cmd[3]) : player.getPlane()));
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::tele coordX coordY plane");
			}
		}
		else if(cmd[0].equals("bank")) {
			player.getBank().initBank();
		}
		else if(cmd[0].equals("coords")) {
			player.getPackets().sendPainelBoxMessage("Coords: "+player.getX()+", "+player.getY()+", "+player.getPlane()+", regionId: "+player.getRegionId()+", rx: "+player.getRegionX()+", ry: "+player.getRegionY());
		}
		else if(cmd[0].equals("spec")) {
			for(int i = 0; i < 10; i++)
			player.getCombatDefinitions().restoreSpecialAttack();
		}
		else if(cmd[0].equals("gfx")) {
			if(cmd.length < 2) {
				player.getPackets().sendPainelBoxMessage("Use: ::gfx id");
				return false;
			}
			try {
				player.setNextGraphics(new Graphics(Integer.valueOf(cmd[1])));
			}catch(NumberFormatException e) {
				player.getPackets().sendPainelBoxMessage("Use: ::gfx id");
			}
		}
		return false;
	}
	
	
	/*
	 * 
	 * 
	 * 
	 * doesnt let it be instanced 
	 */
	private Commands() {
		
	}
}